import { fileURLToPath, URL } from "node:url";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";

export default defineConfig(({ command, mode }) => {
  const config = loadEnv(mode, process.cwd(), "");
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    base: config.VITE_BASE_PATH,
    server: {
      open: true, //项目启动时是否打开页面
      host: "0.0.0.0",
      port: "9000",
      proxy: {
        "^/xaca/": {
          target: config.VITE_API_BASE_URL, // 后台服务器地址
          changeOrigin: true /* 允许跨域 */,
          rewrite: (path) => path.replace(/^\/xaca/, ""),
        },
      },
    },
    //  生产环境
    build: {
      //指定输出路径
      assetsDir: "./",
      // 指定输出文件路径
      outDir: "dist",
      // 代码压缩配置
      terserOptions: {
        // 生产环境移除console
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
  };
});
